package com.gary.archalert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gary.ConsulProviderApp;
import com.gary.entity.Application;
import com.gary.entity.Project;
import com.gary.entity.UnLoad;
import com.gary.entity.XuanYuan;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author peng_rui
 * @date 2022/3/15
 */
public class ApplicationData {
//    public static void main(String[] args) {
//        String xuanyuanJson = getFromJSONFile("xuanyuanApp.json");
//        List<XuanYuan> xuanYuans = JSONArray.parseArray(xuanyuanJson, XuanYuan.class);
//
//        String projectStr = getFromJSONFile("project.json");
//        List<Project> projects = JSONArray.parseArray(projectStr, Project.class);
//        Map<Long, Project> projectMap = projects.stream().collect(Collectors.toMap(Project::getHisId, item -> item));
//
//        String unloadStr = getFromJSONFile("unload.json");
//        List<UnLoad> unLoads = JSONArray.parseArray(unloadStr, UnLoad.class);
//        Set<String> unloadNameSet = unLoads.stream().map(UnLoad::getName).collect(Collectors.toSet());
//
//        ArrayList<Application> addAppList = new ArrayList<>();
//        ArrayList<XuanYuan> cannotDeal = new ArrayList<>();
//        xuanYuans.forEach(xuanYuan -> {
//            Project project = projectMap.get(xuanYuan.getProId());
//            if (project == null) {
//                cannotDeal.add(xuanYuan);
//                return;
//            }
//            if (unloadNameSet.contains(xuanYuan.getName())) {
//                Application app = new Application();
//                app.setName(xuanYuan.getName());
//                app.setIdcName(distinguishIdcName(xuanYuan.getIp()));
//                app.setIp(xuanYuan.getIp());
//                app.setDept(project.getDept());
//                app.setProjectId(project.getId());
//                app.setAlarmSwitch(1);
//                app.setIsPrometheus(0);
//                app.setPort(xuanYuan.getPort());
//                app.setSource(2);
//                app.setPath("/healthCheck");
//                addAppList.add(app);
//            }
//        });
//        System.out.println("===========成功新增=========");
//        String addAppListStr = JSON.toJSONString(addAppList);
//        System.out.println(addAppListStr);
//
//        System.out.println();
//        System.out.println("===========未能新增，未匹配projectId=========");
//        String cannotDelStr = JSON.toJSONString(cannotDeal);
//        System.out.println(cannotDelStr);
//    }
    /**
     * 读取json文件 转成josnStr
     * @return
     */
    public static String getFromJSONFile(String path) {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            Resource resource = new ClassPathResource(path);
            is = resource.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String data = null;
            StringBuffer sb = new StringBuffer();
            while((data = br.readLine()) != null) {
                sb.append(data);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                isr.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String distinguishIdcName(String ip) {
        if (ip.startsWith("172.20")) {
            return "广州机房";
        } else if (ip.startsWith("172.22")) {
            return "深圳机房";
        } else {
            return "未知";
        }
    }
}
